﻿/**
 * Created by Administrator on 2017/3/20.
 */
import axios from 'axios'
import Cookies from 'js-cookie'
import Vue from 'vue'
import {
  lsRead
} from "@/common/js/ls.js";
import {
  router
} from '../router/index.js';

// axios 公共配置
axios.defaults.timeout = 60000;
if (process.env.NODE_ENV === 'production') {
  axios.defaults.baseURL = "refuel/api";
} else {
  //axios.defaults.baseURL = "http://192.168.121.135:8887";
  axios.defaults.baseURL = "http://localhost:8887";
}
axios.defaults.withCredentials = true;

// http request 拦截器
axios.interceptors.request.use(
  config => {
    config.headers.Authorization = lsRead("token")
    config.params = Object.assign({}, config.params, {
      'lang': Cookies.get('language') || 'zh'
    })
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);


// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          if (router.currentRoute.path != '/login') {
            // router.replace({
            //   path: '/login'
            // })
            Vue.prototype.$qiankun_props.router.replace({
              path: '/login'
            })
          }
          break;
      }
    } else {
      router.replace({
        path: '/'
      })
    }
    return Promise.reject(error)
  });
export default axios;
